package com.magus.project.yddemo.service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.magus.framework.core.repository.BaseRepository;
import com.magus.framework.core.service.BaseService;
import com.magus.project.yddemo.entity.IndicatorData;
import com.magus.project.yddemo.entity.IndicatorDataPlant;
import com.magus.project.yddemo.entity.IndicatorDetail;
import com.magus.project.yddemo.repository.IndicatorDataPlantRepository;

/**
 * 指标 Service
 * 
 * @author xujuan
 * @date 2018-01-19 15:26:53
 */
@Service
@Transactional(readOnly = true)
public class IndicatorDataPlantService extends BaseService<IndicatorDataPlant, String> {
	SimpleDateFormat day = new SimpleDateFormat("yyyy-MM-dd");
	SimpleDateFormat month = new SimpleDateFormat("yyyy-MM");
	SimpleDateFormat year = new SimpleDateFormat("yyyy");
	@Autowired
	private IndicatorDataPlantRepository dataPlantRepository;

	@Autowired
	private IndicatorDataService indicatorDataService;

	@Override
	protected BaseRepository<IndicatorDataPlant, String> getBaseRepository() {
		return this.dataPlantRepository;
	}

	public List<IndicatorDataPlant> getIndicatorDataPlantList(List<IndicatorDetail> detailList) {
		List<IndicatorDataPlant> list = new ArrayList<IndicatorDataPlant>();
		IndicatorDataPlant dataPlant = null;
		for (IndicatorDetail detail : detailList) {
			dataPlant = new IndicatorDataPlant();
			dataPlant.setIndicatorDetail(detail);
			dataPlant.setDimension("day");
			list.add(dataPlant);
		}
		return list;
	}

	public List<IndicatorDataPlant> getIndicatorDataPlantMonthList(List<IndicatorDetail> detailList) {
		List<IndicatorDataPlant> list = new ArrayList<IndicatorDataPlant>();
		IndicatorDataPlant dataPlant = null;
		for (IndicatorDetail detail : detailList) {
			dataPlant = new IndicatorDataPlant();
			dataPlant.setIndicatorDetail(detail);
			dataPlant.setDimension("month");
			list.add(dataPlant);
		}
		return list;
	}

	public List<IndicatorDataPlant> getIndicatorDataPlantYearList(List<IndicatorDetail> detailList) {
		List<IndicatorDataPlant> list = new ArrayList<IndicatorDataPlant>();
		IndicatorDataPlant dataPlant = null;
		for (IndicatorDetail detail : detailList) {
			dataPlant = new IndicatorDataPlant();
			dataPlant.setIndicatorDetail(detail);
			dataPlant.setDimension("year");
			list.add(dataPlant);
		}
		return list;
	}

	// 保存日指标填报
	@Transactional
	public List<IndicatorDataPlant> saveIndicatorDataPlantDays(List<IndicatorDataPlant> dataPlants, String date) {
		Date saveTime = new Date();
		try {
			saveTime = day.parse(date);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		for (IndicatorDataPlant plant : dataPlants) {
			plant.setTime(saveTime.getTime());
		}
		dataPlants = dataPlantRepository.save(dataPlants);

		return dataPlants;
	}

	// 上传日指标填报
	@Transactional
	public List<IndicatorData> reportIndicatorDataDays(List<IndicatorDataPlant> dataPlants, String date) {
		List<IndicatorData> datas = new ArrayList<IndicatorData>();

		Date saveTime = new Date();
		try {
			saveTime = day.parse(date);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		IndicatorData indicatorData = null;
		for (IndicatorDataPlant plant : dataPlants) {
			indicatorData = new IndicatorData();

			indicatorData.setBatchCode(plant.getBatchCode());
			indicatorData.setDimension(plant.getDimension());
			indicatorData.setIndicatorDetail(plant.getIndicatorDetail());
			indicatorData.setTime(saveTime.getTime());
			indicatorData.setValue(plant.getValue());

			datas.add(indicatorData);
		}
		datas = indicatorDataService.save(datas);

		return datas;
	}

	// 保存月指标填报
	@Transactional
	public List<IndicatorDataPlant> saveIndicatorDataPlantMonths(List<IndicatorDataPlant> dataPlants, String date) {
		Date saveTime = new Date();
		try {
			saveTime = month.parse(date);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		for (IndicatorDataPlant plant : dataPlants) {
			plant.setTime(saveTime.getTime());
		}
		dataPlants = dataPlantRepository.save(dataPlants);

		return dataPlants;
	}

	// 上传月指标填报
	@Transactional
	public List<IndicatorData> reportIndicatorDataMonths(List<IndicatorDataPlant> dataPlants, String date) {
		List<IndicatorData> datas = new ArrayList<IndicatorData>();

		Date saveTime = new Date();
		try {
			saveTime = month.parse(date);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		IndicatorData indicatorData = null;
		for (IndicatorDataPlant plant : dataPlants) {
			indicatorData = new IndicatorData();

			indicatorData.setBatchCode(plant.getBatchCode());
			indicatorData.setDimension(plant.getDimension());
			indicatorData.setIndicatorDetail(plant.getIndicatorDetail());
			indicatorData.setTime(saveTime.getTime());
			indicatorData.setValue(plant.getValue());

			datas.add(indicatorData);
		}
		datas = indicatorDataService.save(datas);

		return datas;
	}

	// 保存年指标填报
	@Transactional
	public List<IndicatorDataPlant> saveIndicatorDataPlantYears(List<IndicatorDataPlant> dataPlants, String date) {
		Date saveTime = new Date();
		try {
			saveTime = year.parse(date);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		for (IndicatorDataPlant plant : dataPlants) {
			plant.setTime(saveTime.getTime());
		}
		dataPlants = dataPlantRepository.save(dataPlants);

		return dataPlants;
	}

	// 上传年指标填报
	@Transactional
	public List<IndicatorData> reportIndicatorDataYears(List<IndicatorDataPlant> dataPlants, String date) {
		List<IndicatorData> datas = new ArrayList<IndicatorData>();

		Date saveTime = new Date();
		try {
			saveTime = year.parse(date);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		IndicatorData indicatorData = null;
		for (IndicatorDataPlant plant : dataPlants) {
			indicatorData = new IndicatorData();

			indicatorData.setBatchCode(plant.getBatchCode());
			indicatorData.setDimension(plant.getDimension());
			indicatorData.setIndicatorDetail(plant.getIndicatorDetail());
			indicatorData.setTime(saveTime.getTime());
			indicatorData.setValue(plant.getValue());

			datas.add(indicatorData);
		}
		datas = indicatorDataService.save(datas);

		return datas;
	}
}
